package com.xls.box2023.jinbi.jifenduihuanjilu;

import android.text.TextUtils;

import com.lzz.bottomencapsulation.consts.Consts;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.xls.box2023.Consts.IntentConsts;
import com.xls.box2023.adapter.JiFenDuiHuanJiLuAdapter;
import com.xls.box2023.entity.JiFenDuiHuanJiLuEntity;
import com.xls.box2023.entity.PageEntity;
import com.xls.box2023.jinbi.JinBiNetWorkUtil;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import androidx.recyclerview.widget.RecyclerView;

/**
 * MVPPlugin
 */

public class JiFenDuiHuanJiLuPresenter extends BasePresenterImpl<JiFenDuiHuanJiLuContract.View> implements JiFenDuiHuanJiLuContract.Presenter{
    JiFenDuiHuanJiLuAdapter jiFenDuiHuanJiLuAdapter;
    ArrayList<JiFenDuiHuanJiLuEntity> jiFenDuiHuanJiLuDataList=new ArrayList<>();
    int pageSize= Consts.PageSize,pageNum=Consts.PageNum;
    @Override
    public RecyclerView.Adapter getAdapter() {
        if (jiFenDuiHuanJiLuAdapter==null){
            jiFenDuiHuanJiLuAdapter=new JiFenDuiHuanJiLuAdapter(context,jiFenDuiHuanJiLuDataList,getType());
        }
        return jiFenDuiHuanJiLuAdapter;
    }

    @Override
    public XRecyclerView.LoadingListener getLoadingListener() {
        return new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageNum=Consts.PageNum;
                getSourceData();
            }

            @Override
            public void onLoadMore() {
                pageNum=pageNum+1;
                getSourceData();
            }
        };
    }
    public String getType() {
        return activity.getIntent().getStringExtra(IntentConsts.type);
    }
    /**
     * 获取页面数据
     */
    @Override
    public void getSourceData() {
        super.getSourceData();
        JinBiNetWorkUtil.getInstance().getMyScoreExchangeRecords(getType(),pageSize, pageNum, getAppID(),new RequestCallBack<PageEntity<JiFenDuiHuanJiLuEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {
                if (mView==null||context==null)return;
                mView.getListView().refreshComplete();
                mView.getListView().loadMoreComplete();
            }

            @Override
            public void onRequestSuccess(PageEntity<JiFenDuiHuanJiLuEntity> requestData) {
                if (mView==null||context==null||requestData==null)return;
                if (pageNum==Consts.PageNum){
                    jiFenDuiHuanJiLuDataList.clear();
                }
                if (requestData.getRecords()!=null) {
                    jiFenDuiHuanJiLuDataList.addAll(requestData.getRecords());
                }
                if (jiFenDuiHuanJiLuDataList.size()>=requestData.getTotal()){
                    mView.getListView().setNoMore(true);
                }else {
                    mView.getListView().setNoMore(false);
                }
                jiFenDuiHuanJiLuAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    private String getAppID() {
        String appID=activity.getIntent().getStringExtra(IntentConsts.data);
        if (TextUtils.isEmpty(appID))return "";
        return appID;
    }
}
